package com.flycent.hqhs.ui;

import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.annotation.view.ViewInject;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.flycent.hqhs.R;
import com.flycent.hqhs.http.WebClient;
import com.flycent.hqhs.http.WebException;
import com.flycent.hqhs.util.ContextUtil;

/**
 * @author fbl
 * 忘记密码第二步，获取验证码
 */
public class GetCodeActivity extends BackableTitleBarActivity{
	@ViewInject(id = R.id.userName_text ) TextView mUserNameText;
	@ViewInject(id = R.id.code_edit ) EditText mCodeEdit;
	@ViewInject(id = R.id.getCodeBut , click = "onClick") TextView mGetCodeText;
	@ViewInject(id = R.id.next_but , click = "onClick") Button mNextBut;

	private String mUserName;
	private TimeCount time;
	public static GetCodeActivity instance = null;
	
	@Override
	protected void onCreate(Bundle savedInstance) {
		super.onCreate(savedInstance);
		setContentView(R.layout.activity_getcode);
		FinalActivity.initInjectedView(this);
		getTitleBar().setTitle(R.string.get_the_code);
		instance = this;

		time = new TimeCount(60000, 1000);//构造CountDownTimer对象
		mUserName = getIntent().getStringExtra("userName");
		mUserNameText.setText(mUserName);
		time.start();//开始计时

	}

	public void onClick(View v){
		/** 获取验证码 */
		if( v == mGetCodeText){
			getCode(mUserName);
		}
		/** 下一步 */
		if( v == mNextBut){
			if( invalidateText(mCodeEdit,R.string.txt_info_verify_no)){
				return;
			}
			
			Bundle bundle = new Bundle();
			bundle.putString("code", getText(mCodeEdit));
			bundle.putString("userName", mUserName);
			ContextUtil.startActivity(this, ResetPwdActivity.class,bundle);
			
		}
	}

	private void getCode(final String userName){
		new Thread(){
			public void run() {
				try {
					WebClient.getInstance(GetCodeActivity.this).getCodeWithoutPicture(userName);
					mHandler.sendEmptyMessage(0);
				} catch (WebException e) {
					e.printStackTrace();
				}
			};
		}.start();
	}
	
	@SuppressLint("HandlerLeak") 
	private Handler mHandler = new Handler(){
		public void dispatchMessage(Message msg) {
			switch (msg.what) {
			case 0:
				time = new TimeCount(60000, 1000);//构造CountDownTimer对象
				time.start();//开始计时
				break;
			default:
				break;
			}
		};
		
	};
	
	

	/* 定义一个倒计时的内部类 */
	class TimeCount extends CountDownTimer {
		public TimeCount(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);//参数依次为总时长,和计时的时间间隔
		}
		@Override
		public void onFinish() {//计时完毕时触发
			mGetCodeText.setText("获取验证码");
			mGetCodeText.setClickable(true);
		}
		@Override
		public void onTick(long millisUntilFinished){//计时过程显示
			mGetCodeText.setClickable(false);
			mGetCodeText.setText(millisUntilFinished /1000+"秒后重新获取");
		}
	}

}
